/**
 * 获得搜索参数
 * @param params
 * @returns {Object}
 */
function getSearchParams(params) {
    var searchParams = new Object();
    if (params != undefined) {
        searchParams = params;
    }
    $('.SearchForm').each(function() {
        var param = $(this).val().trim();
        var val = $("input:radio[name='sex']:checked").val();
        if (val == 0) {
            searchParams['sex'] = 0;
        } else if (val == 1) {
            searchParams['sex'] = 1;
        }
        if (param == undefined)
            param = '';
        searchParams[$(this).attr('id')] = param;
    });
    return searchParams;
}
//清空搜索条件
$("#Reset").click(function() {
    $('.SearchForm').val('');
    $("input:radio[name='sex']").removeAttr('checked');
});
/**
 * 页面自加载
 */
$(function() {
    // $('#student_createtime').datepicker({
    //     format: "yyyy-mm-dd",
    //     todayBtn: "linked",
    //     autoclose: true,
    //     todayHighlight: true,
    //     clearBtn: true
    // });

    var cellwidth = ($(".box-content.table-responsive").width() - 55) / 11;
    var psval = $('#datatable_studentinfo').attr('data-size');
    if (psval == undefined || psval == "") {
        psval = 10;
    }
    /**
     * easyui 生成表格数据
     */
    $('#datatable_studentinfo').datagrid({
        singleSelect: true, //允许选择多行
        striped: true,
        idField: 'stu_id',
        remoteSort: false,
        collapsible: true,
        fit: false,
        url: 'admin/Student/getStudents', //数据源路径
        loadMsg: '请等待数据载入....',
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        pageSize: psval,
        pageList: [psval, psval * 2, psval * 3, psval * 4, psval * 5],
        columns: [[{
                    field: 'stu_nickname',
                    title: '昵称',
                    align: 'center',
                    width: cellwidth,
                    sortable: true
                }, {
                    field: 'stu_sex',
                    title: '性别',
                    align: 'center',
                    width: cellwidth,
                    sortable: true,
                    formatter:function(value){
                        if(value==1){
                            return "男";
                        }
                        if(value==0){
                            return "女";
                        }
                    }
                }, {
                    field: 'stu_phone',
                    title: '电话',
                    align: 'center',
                    width: cellwidth,
                    sortable: true
                }, {
                    field: 's_name',
                    title: '课程',
                    align: 'center',
                    width: cellwidth,
                    sortable: true
                }, {
                    field: 'stu_kouyu',
                    title: '口语余额',
                    align: 'center',
                    width: cellwidth,
                    sortable: true
                }, {
                    field: 'stu_zuowen',
                    title: '作文余额',
                    align: 'center',
                    width: cellwidth,
                    sortable: true
                }, {
                    field: 'stu_ziliaostatus',
                    title: '资料提交',
                    align: 'center',
                    width: cellwidth,
                    sortable: true,
                    formatter:function(value){
                        if(value==1){
                            return "<span style='color: green;'>是</span>";
                        }
                        if(value==0){
                            return "<span style='color: red;'>否</span>";
                        }
                    }
                },{
                field: 'stu_id',
                title: '操作',
                align: 'center',
                width: cellwidth,
                sortable: true,
                formatter:function(value,row,index){
                    return "<a onclick='chooseTeacher(this);' name='"+value+"' href='JavaScript:;'>指定老师</a>";
                }
            }]],
        onBeforeLoad: function(param) {
            param = getSearchParams(param);
        },
    });


    $('#Search').click(function() {
        $('#datatable_studentinfo').datagrid('reload');
    })


    /**
     * 删除
     */
    $('.removebook').click(function() {
        var row = $('#datatable_studentinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一行数据！',
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '只能选择一行！',
            });
            return;
        }
        $.TeachDialog({
            content: "确认删除此学生？",
            showCloseButton: true,
            showCloseButtonName: '取消',
            otherButtons: ['确认'],
            otherButtonStyles: ['btn-danger'],
            clickButton: function(sender, modal, index) {
                modal.modal('hide');
                $.ajax({
                    url: 'admin/Student/deleteByID', //form action
                    dataType: 'JSON', //返回体类型
                    type: 'POST', // form type
                    data: "id=" + row[0].stu_id,
                    success: function(data) {
                        if (data.status == 1) {
                            $.TeachDialog({
                                content: data.msg,
                            });
                            $('#datatable_studentinfo').datagrid('reload');
                            $('#datatable_studentinfo').datagrid('uncheckAll');
                        } else {
                            $.TeachDialog({
                                content: '删除失败',
                            });
                        }
                    },
                    error: function() {
                        $.TeachDialog({
                            content: '删除失败',
                        });
                    }
                });
            }
        })


    })


    /**
     * 编辑
     */
    $(".editbook").click(function() {
        var row = $('#datatable_studentinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一行数据进行修改！',
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '只能选择一行进行修改！',
            });
            return;
        }
        //获得修改框内容
        var content = "";
        $.ajax({
            url: 'admin/Student/updatemodal',
            data: "stu_id=" + row[0].stu_id,
            type: 'POST',
            dataType: 'HTML', //返回的数据类型
            success: function(updatemodalhtml) {
                content = updatemodalhtml;
                //弹出修改框
                $.TeachDialog({
                    modalId: null,
                    animation: null,
                    title: '修改学生信息',
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    CloseButtonAddFunc: function() {
                    },
                    otherButtons: ['修改'],
                    otherButtonStyles: [],
                    bootstrapModalOption: {
                        backdrop: 'static'
                    },
                    largeSize: false,
                    smallSize: false,
                    dialogShow: function() {
                    },
                    dialogShown: function() {
                    },
                    dialogHide: function() {
                    },
                    dialogHidden: function() {
                    },
                    clickButton: function(sender, modal, index) {
                        $.ajax({
                            url: 'admin/Student/update',
                            data: $("#updateForm").serialize(),
                            type: 'POST',
                            dataType: 'JSON',
                            success: function(returnData) {
                                if (returnData.status == 0) {
                                    $.TeachDialog({
                                        content: returnData.msg,
                                        showCloseButton: false,
                                        CloseButtonAddFunc: function() {

                                        }
                                    });
                                } else {
                                    modal.modal('hide');
                                    $.TeachDialog({
                                        content: returnData.msg,
                                        showCloseButton: false,
                                    });
                                    $('#datatable_studentinfo').datagrid('reload');
                                }
                            },
                            error: function() {
                                $.TeachDialog({
                                    content: '修改失败',
                                });
                            },
                        });
                    },
                });
            },
            error: function() {
                $.TeachDialog({
                    content: '获取数据失败，无法进行修改',
                });
                return;
            }
        });

    });
    /**
     * 查看学生详情
     */
    $(".addbook").click(function() {
        var row = $('#datatable_studentinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一个学生进行查看！'
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '请选择一个学生进行查看！',
            });
            return;
        }
        //获得修改框内容
        var content = "";
        $.ajax({
            url:"admin/Student/seeinfo",
            data:"stu_id=" + row[0].stu_id,
            type:'POST',
            dataType:'HTML',
            success:function(returndata){
                content = returndata;
                $.TeachDialog({
                    modalId: null,
                    animation: null,
//                        largeSize:true,
                    title: "<h2>学生详情</h2>",
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    CloseButtonAddFunc: function () {
                    }
                });
            },
            error:function(){
                alert('数据返回出错，无伤大雅，请继续');
            }
        });

    });
    /*
     * 导入老师信息excel表
     */
    // var uploadOption =
    //         {
    //             // 提交目标
    //             action: "admin/student/import",
    //             // 服务端接收的名称
    //             name: "Filedata",
    //             // 自动提交
    //             autoSubmit: false,
    //             // 选择文件之后…
    //             onChange: function(file, extension) {
    //                 if (new RegExp(/(xls)/i).test(extension)) {
    //                     $("#state").val(file);
    //                 } else {
    //                     $.ajax({
    //                         url: 'admin/admin/alertlog',
    //                         data: "t=3" ,
    //                         type: 'POST',
    //                         dataType: 'HTML', //返回的数据类型
    //                         success: function(updatemodalhtml) {
    //                             content = updatemodalhtml;
    //                             //弹出修改框
    //                             $.TeachDialog({
    //                                 modalId: null,
    //                                 animation: null,
    //                                 title: '系统消息',
    //                                 content: content,
    //                                 showCloseButton: true,
    //                                 showCloseButtonName: '关闭',
    //                                 CloseButtonAddFunc: function() {
    //                                 },
    //
    //                                 otherButtonStyles: [],
    //                                 bootstrapModalOption: {
    //                                     backdrop: 'static'
    //                                 },
    //                                 largeSize: false,
    //                                 smallSize: false,
    //                             });
    //                         },
    //                         error: function() {
    //                             $.TeachDialog({
    //                                 content: '获取数据失败，无法进行修改',
    //                             });
    //                             return;
    //                         }
    //                     });
    //                 }
    //             },
    //             // 开始上传文件
    //             onSubmit: function(file, extension) {
    //                 $("#state").val("正在上传" + file + "..");
    //             },
    //             // 上传完成之后
    //             onComplete: function(file, response) {
    //
    //                 $.ajax({
    //                     url: 'admin/admin/alertlog',
    //                     data: "t=2" ,
    //                     type: 'POST',
    //                     dataType: 'HTML', //返回的数据类型
    //                     success: function(updatemodalhtml) {
    //                         content = updatemodalhtml;
    //                         //弹出修改框
    //                         $.TeachDialog({
    //                             modalId: null,
    //                             animation: null,
    //                             title: '系统消息',
    //                             content: content,
    //                             showCloseButton: true,
    //                             showCloseButtonName: '关闭',
    //                             CloseButtonAddFunc: function() {
    //                             },
    //
    //                             otherButtonStyles: [],
    //                             bootstrapModalOption: {
    //                                 backdrop: 'static'
    //                             },
    //                             largeSize: false,
    //                             smallSize: false,
    //                         });
    //                     },
    //                     error: function() {
    //                         $.TeachDialog({
    //                             content: '获取数据失败，无法进行修改',
    //                         });
    //                         return;
    //                     }
    //                 });
    //                 $("#state").val("");
		// 			$('#datatable_studentinfo').datagrid('reload');
    //             }
    //         }
    //
    // // 初始化excel上传
    // var oAjaxUpload = new AjaxUpload('#selector', uploadOption);

    // 给上传按钮增加上传动作
    $("#up").click(function()
    {
        var content = "";
		if($("#state").val().length==0){
            $.ajax({
                url: 'admin/admin/alertlog',
                data: "t=1" ,
                type: 'POST',
                dataType: 'HTML', //返回的数据类型
                success: function(updatemodalhtml) {
                    content = updatemodalhtml;
                    //弹出修改框
                    $.TeachDialog({
                        modalId: null,
                        animation: null,
                        title: '系统消息',
                        content: content,
                        showCloseButton: true,
                        showCloseButtonName: '关闭',
                        CloseButtonAddFunc: function() {
                        },

                        otherButtonStyles: [],
                        bootstrapModalOption: {
                            backdrop: 'static'
                        },
                        largeSize: false,
                        smallSize: false,
                    });
                },
                error: function() {
                    $.TeachDialog({
                        content: '获取数据失败，无法进行修改',
                    });
                    return;
                }
            });
		}
        oAjaxUpload.submit();
    });




});
